package com.example.myapplication.rxjava;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.R;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import io.reactivex.Maybe;
import io.reactivex.Observable;
import io.reactivex.Single;
import io.reactivex.SingleObserver;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;

/**
 * 类描述:
 * 创建人:   tangchao$
 * 创建时间: 2021/8/24$ 8:49 AM$
 * 联系方式: 419704299@qq.com
 * 修改人:   tangchao$
 * 修改时间: 2021/8/24$ 8:49 AM$
 * 修改备注:  [说明本次修改内容]
 */
public class Rxjava2Activity2 extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rxjava2activity);
        test1();
    }

    private void test1() {
        List aa = new ArrayList();
        Observable.fromArray(aa).toList();

//        Observable.just(1, 2, 3, 4).toList().subscribe(new Consumer<Integer>() {
//            @Override
//            public void accept(Integer o) throws Exception {
//
//            }
//        });

//        Observable.just(1, 2, 3).takeLast(1).subscribe();
//        Observable.just(1, 2, 3).first().subscribe();
        Observable.just(1, 2, 3).all(new Predicate<Integer>() {
            @Override
            public boolean test(@NonNull Integer integer) throws Exception {
                return integer > 5;
            }
        }).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean o) throws Exception {

            }
        });

        Observable.just(1, 2, 3).contains(4).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean o) throws Exception {

            }
        });

        Observable.just(1, 2, 3, 4).toList()
                .map(new Function<List<Integer>, Object>() {
                    @Override
                    public Object apply(@NonNull List<Integer> integers) throws Exception {
                        return new RuntimeException();
                    }
                })
//                .retryWhen()
                .onErrorReturn(new Function<Throwable, Object>() {
                    @Override
                    public Object apply(@NonNull Throwable throwable) throws Exception {
                        return "onErrorReturn：返回单个对象";
                    }
                })
                .onErrorResumeNext(new Single<Object>() {
                    @Override
                    protected void subscribeActual(@NonNull SingleObserver<? super Object> observer) {
                        observer.onError(new Throwable("当抛出Throwable时返回一个新的Observable"));
                    }
                }).retry(2 )
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        Logger.d("Rxjava2Activity2===accept: ");

                    }
                });

        Single.just("11").toFlowable().toMap(new Function<String, String>() {
            @Override
            public String apply(@NonNull String s) throws Exception {
                return "key" + s;
            }
        }).subscribe(new Consumer<Map<String, String>>() {
            @Override
            public void accept(Map<String, String> stringStringMap) throws Exception {
                Logger.d("Rxjava2Activity2===accept: " + stringStringMap);

            }
        });
        Observable.just(1, 2, 3).cast(String.class).subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {
                Logger.d("Rxjava2Activity2===accept: cast在发射之前强制将Observable发射的所有数据转换为指定类型");

            }
        });


        Maybe.just(1).subscribe(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) throws Exception {

            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {

            }
        });
    }
}
